import java.util.Arrays;

public class MyArraysList implements IList{
    public int[] elam;
    public int Usersize;

    public MyArraysList() {
        this.elam = new int[Constant.DEFALL_T_CAPACITY];
    }

    @Override
    public void add(int data) {
        if(isFull()){
           grow();
        }
        this.elam[this.Usersize] = data;
        this.Usersize ++;
    }
    private void grow(){
        this.elam = Arrays.copyOf(this.elam,this.elam.length * 2);
    }
    public boolean isFull(){
        return this.Usersize == this.elam.length;
    }

    @Override
    public void add(int pos, int data) {
        if(isFull()){
            grow();
        }
        checkPosAdd(pos,"pos不合法");
        if(pos == Usersize){
            elam[pos] = data;
            Usersize++;
            return;
        }
        for (int i = Usersize - 1; i >= pos; i--) {
            elam[i + 1] = elam[i] ;
        }
        elam[pos] = data;
        Usersize++;
        return;
    }
    private void checkPosAdd(int pos,String msg){
        if(pos < 0 || pos > Usersize){
            throw new PosIllegaLity(msg);
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < elam.length; i++) {
            if(elam[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < elam.length; i++) {
            if(elam[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    private void checkPos(int pos,String msg){
        if(pos < 0 || pos >= Usersize){
            throw new PosIllegaLity(msg);
        }
    }
    private boolean isEmpty(){
        return Usersize == 0;
    }
    @Override
    public int get(int pos) {
        if(isEmpty()){
           throw new EmptyllegalLity("这是空的");
        }
        checkPos(pos,"get方法的pos位置不合法");
        return elam[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(isEmpty()){
            throw new EmptyllegalLity("这是空的");
        }
        checkPos(pos,"set的pos位置非法");
        elam[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if(isEmpty()){
            throw new EmptyllegalLity("这是空的");
        }
        int index = indexOf(toRemove);
        for (int i = index; i < Usersize; i++) {
            elam[i] = elam[i + 1];
        }
        Usersize--;
    }

    @Override
    public int size() {
        return this.Usersize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < Usersize; i++) {
            elam[i] = 0;
        }
        Usersize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < elam.length; i++) {
            System.out.print(elam[i] + " ");
        }
        System.out.println();
    }
}
